<?php

namespace App\Http\Controllers\Home;

use App\Http\Model\Article;
use App\Http\Model\Cate;
use App\Http\Model\Link;
use App\Http\Model\Nav;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class IndexController extends CommonController
{
    /**
     * 前台首页
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //前台首页需要的数据

//       共享数据放在了CommonContrller控制器的构造函数中生成
//        4 推荐文章  6篇   $recommand
        $recommand = Article::orderBy('art_view','desc')->take(6)->get();
//        5 图文文章  5篇  $pics 带分页
        $pics = Article::orderBy('art_view','desc')->paginate(5);
//        6 友情链接   3个 $link
        $link = Link::take(3)->get();

        return view('home.index',compact('recommand','pics','link'));
    }

//    前台列表页
    public function lists($id)
    {
//        1 当前分类的信息   $cate
          $cate =  Cate::find($id);
//        2 当前分类的子类  4个  $subcate
          $subcate =  Cate::where('cate_pid',$id)->take(4)->get();
//        3 当前分类下四篇带分页文章  $arts
          $arts = Article::where('cate_id',$id)->paginate(2);

//          当前分类的访问量加一
        Cate::where('cate_id',$id)->increment('cate_view');
        return view('home.list',compact('cate','subcate','arts'));
    }

//     前台详情页
    public function news($id)
    {
//          1 查询当前文章及分类
//        join('要链接的表', '当前表跟连接表关联的字段', '=', '被链接表.外键')
       $arts =   Article::where('art_id',$id)->join('category','article.cate_id','=','category.cate_id')->first();
//         2 上一篇 下一篇文章  654321
       $pre =  Article::orderBy('art_id','desc')->where('art_id','<',$id)->first();

//        下一篇
       $next = Article::orderBy('art_id','asc')->where('art_id','>',$id)->first();

//         3 相关文章  找到同分类下的4篇文章
       $same =  Article::where('cate_id',$arts->cate_id)->take(4)->get();

//       4 文章访问量+1
        Article::where('art_id',$id)->increment('art_view');
        return view('home.new',compact('arts','pre','next','same'));
    }
}
